//使用友元（friend）函数方式，实现了 —（减法）、>>、<<重载
//从文件中读取2个复数，计算差后输出
#include <iostream>
#include <fstream>
using namespace std;
class Complex
{
public:
    Complex(double r=0,double i=0):re(r),im(i) {}

    friend Complex operator-(Complex cm1,Complex cm2);
    friend istream &operator >> (istream &in, Complex &cm);
    friend ostream &operator<<(ostream &out, Complex cm);

private:
    double re;//实部
    double im; //虚部
};

Complex operator-(Complex cm1,Complex cm2)
{
    Complex cm;
    cm.re = cm1.re - cm2.re;
    cm.im = cm1.im - cm2.im;
    return cm;
}
ostream &operator<<(ostream &out, Complex cm)
{
    out << cm.re << "+" << cm.im << "i" ;
    return out;
}

istream &operator >>(istream &in, Complex &cm)
{
    in >> cm.re >> cm.im;
    return in;
}

int main()
{
    Complex cm1, cm2, cm3;
    ifstream fin("file_src.txt");
    if (fin.fail())
        exit(1);
    fin >> cm1 >> cm2;
    fin.close();

    cm3 = cm1 - cm2;

    ofstream fout("file_dst.txt");
    if (fout.fail())
        exit(1);
    fout << cm1 << " - "
         << cm2 << "="
         << cm3 << endl;
    fout.close();
    return 0;
}
